package com.loong.system.handler;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.utils.TransformUtils;
import com.loong.system.business.SystemFeedbackTypeBusiness;
import com.loong.system.common.backend.param.SystemFeedbackTypeParam;
import com.loong.system.common.backend.param.SystemFeedbackTypeQueryParam;
import com.loong.system.common.backend.result.SystemFeedbackTypeResult;
import com.loong.system.common.forehead.result.SystemFeedbackTypeForeheadResult;
import com.loong.system.model.SystemFeedbackType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author LiuQun
 * @date 2020-09-29 21:57
 */
@Service
public class SystemFeedbackTypeHandler {
    @Resource
    private SystemFeedbackTypeBusiness feedbackTypeBusiness;

    @Resource
    private BatchExecutor batchExecutor;

    @Resource
    private SystemFeedbackHandler feedbackHandler;

    /**
     * 后台新增用户反馈类型
     * @param param 用户反馈类型参数
     */
    public void saveFeedbackType(SystemFeedbackTypeParam param){
        param.setCreateTime(new Date());
        feedbackTypeBusiness.save(TransformUtils.transform(param, SystemFeedbackType.class));
    }

    /**
     * 后台批量删除用户反馈类型
     * @param idList 用户反馈类型id集合
     */
    public void batchDeleteFeedbackType(List<Long> idList){
        batchExecutor.batchExecute(idList,id -> id.toString(),(item) -> {
            //查询用户反馈信息中是否使用到了该反馈类型，若是则不能删除，否则可以删除
            boolean existFeedback = feedbackHandler.existsFeedbackByFeedbackType(item);
            if (existFeedback){
                throw new GlobalException("feedback type is used");
            }else {
                feedbackTypeBusiness.deleteById(item);
            }
        });
    }

    /**
     * 后台更新用户反馈类型
     * @param param 反馈类型参数
     */
    public void updateFeedbackType(SystemFeedbackTypeParam param){
        param.setLastUpdateTime(new Date());
        feedbackTypeBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param,SystemFeedbackType.class));
    }

    /**
     * 后台分页查询用户反馈类型
     * @param param 反馈类型查询参数
     * @return 分页结果
     */
    public Pager<SystemFeedbackTypeResult> pageQueryFeedbackType(SystemFeedbackTypeQueryParam param){
        Pager<SystemFeedbackType> pager = feedbackTypeBusiness.pageQuery(param, () -> feedbackTypeBusiness.findFeedbackTypeByParam(param));
        return TransformUtils.transformPage(pager,SystemFeedbackTypeResult.class);
    }

    /**
     * 前台分页查询用户反馈类型
     * @param query 查询参数
     * @return 分页结果
     */
    public Pager<SystemFeedbackTypeForeheadResult> foreheadPageQueryFeedbackType(BaseQuery query){
        Pager<SystemFeedbackType> pager = feedbackTypeBusiness.pageQuery(query, () -> feedbackTypeBusiness.findAll());
        return TransformUtils.transformPage(pager,SystemFeedbackTypeForeheadResult.class);
    }
}
